//一个稳定的序列是可以实现不稳定的排序的
//一个不稳定的序列是不可能做到稳定的排序的

import java.util.Arrays;
import java.util.Random;


//当给出一个基本有序的序列时，使用直接插入排序更快
public class Sort {  //直接插入排序  是稳定的排序
    public static void insertSort(int[] array){
        int tmp = 0;
        for (int i = 1; i < array.length; i++) {
            tmp = array[i];
            int j = i-1;
            for (; j >= 0; j--) {
                if (array[j] > tmp){
                    array[j+1] = array[j];
                }else {
//                    array[j+1] = tmp;
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    public static void testInsertSort(int[] array) {
        int[] tmpArray = Arrays.copyOf(array, array.length);
        long startTime = System.currentTimeMillis(); //记录开始的时间
        Sort.insertSort(tmpArray);
        long endTime = System.currentTimeMillis(); //记录结束的时间
        System.out.println("插入排序耗时：" + (endTime - startTime));
    }
    public static void notOrderArrayRandom(int[] array){ //数组以内生成随机数字序列
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(100000);//100000以内的随机数字
        }
    }



    public static void shellSort(int[] array){  //希尔排序
        int gap = array.length;
        while (gap > 1){
            gap = gap / 2;
            shell(array,gap);
        }
    }
    private static void shell(int[] array,int gap){
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >=0 ; j -= gap) {
                if (array[j] > tmp){
                    array[j+gap] = array[j];
                }else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }
    public static void testShellSort(int[] array) {
        int[] tmpArray = Arrays.copyOf(array, array.length);
        long startTime = System.currentTimeMillis(); //记录开始的时间
        Sort.shellSort(tmpArray);
        long endTime = System.currentTimeMillis(); //记录结束的时间
        System.out.println("插入排序耗时：" + (endTime - startTime));
    }



    public static void choiceSort(int[] array){  //选择排序
        for (int i = 0; i < array.length; i++) {
            int a = i;
            for (int j = i+1; j < array.length; j++) {
                if (array[j] < array[a]){
                    a = j;
                }
            }
            swap(array,a,i);
        }
    }
    private static void swap(int[] array,int a,int b){
        int tmp = array[a];
        array[a] = array[b];
        array[b] = tmp;
    }
    public static void choiceSort1(int[] array){  //选择排序，直接确定最大值和最小值的位置
        int left = 0;
        int right = array.length-1;
        while (left < right){
            int minIndex = left;
            int maxIndex = left;
            for (int i = left+1; i <= right; i++) {
                if (array[i] < array[minIndex]){
                    minIndex = i;
                }
                if (array[i] > array[maxIndex]){
                    maxIndex = i;
                }
            }
            swap(array,minIndex,left);
            if (maxIndex == left){
                maxIndex = minIndex;
            }
            swap(array,maxIndex,right);

            left++;
            right--;
        }
    }


    public static void heapSort(int[] array){
        buildBigHeap(array);

        int end = array.length-1;
        while (end > 0){
            swap(array,0,end);
            siftDown(array,0,end);
            end--;
        }

    }
    private static void buildBigHeap(int[] array){  //堆排序
        for (int parent = (array.length-1-1)/2; parent >= 0; parent--) {
            siftDown(array,parent,array.length);
        }
    }
    private static void siftDown(int[] array,int parent,int end){
        int child = parent*2+1;
        while (child < end){
            if (child + 1 < end && array[child] < array[child+1]){
                child++;
            }
            if (array[child] > array[parent]){
                swap(array,child,parent);
                parent = child;
                child = parent*2+1;
            }else {
                break;
            }
        }
    }



    public static void bubbleSort(int[] array){ //冒泡排序
        for (int i = 0; i < array.length-1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length-1-i; j++) { //每排序一次后面比较次数都要减一
                if (array[j] > array[j+1]){
                    swap(array,j,j+1);
                    flg = true;
                }
            }
            if (flg == false){
                return;
            }
        }
    }


    public static void quickSort(int[] array){  //快速排序
        quick(array,0,array.length-1);
    }
    public static void quick(int[] array,int start,int end){
        if (start >= end){
            return;
        }
        int pivot = partition(array,start,end);
        quick(array,start,pivot-1);
        quick(array,pivot+1,end);
    }
    private static int partition(int[] array,int left,int right){
        int key = array[left];
        int i = left;
        while (left < right){
            while (left < right && array[right] >= key){
                right--;
            }
            while (left < right && array[left] <= key){
                left++;
            }
            swap(array,left,i);
        }
        return left;
    }
}
